#!/usr/bin/python2.5
#
# Copyright 2010 Google Inc.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

import gzip
import logging
import package
import re
import StringIO
import urllib2
import wsgiref.handlers
from google.appengine.ext import db
from google.appengine.ext import webapp

# TODO(stratus): Read multiple URLs from yaml.
#url = 'http://packages.ubuntu.com/hardy/allpackages?format=txt.gz'
url = 'http://64.233.163.132/search?q=cache:pQ_IBDSeNRQJ:packages.ubuntu.com/hardy/allpackages+allpackages+site:packages.ubuntu.com'


# TODO(stratus): Extend regex.
pattern = re.compile(r'^(\S+) \((\S+)\) (.+)$')

class PackageFetcher(webapp.RequestHandler):
  def get(self):
    packagefetcher = package.Package()
    
    try:
      logging.debug('packagefetcher: trying to download %s', url)
      result = urllib2.urlopen(url)
    except urllib2.URLError, error:
      logging.error('packagefetcher: error %s while trying to download %s', error.reason, url)

    compressed_stream = StringIO.StringIO(result.read())
    fd = gzip.GzipFile(fileobj=compressed_stream)
    for line in fd:
      logging.debug('packagefetcher: %s', line)
      match = pattern.search(line)
    if match:
      packagefetcher.name = match.group(0)
      packagefetcher.version = match.group(1)
      packagefetcher.description = match.group(2)
      query = db.GqlQuery('SELECT * FROM Package WHERE name = :1 AND version :2', name, version)
      results = query.fetch(10) # should be 1
      for result in results:
	logging.debug('packagefetcher: trying to delete older entry for %s %s', name, version)
	result.delete()
      logging.debug('packagefetcher: trying to put %s %s %s', name, version, description)
      packagefetcher.put()
    else:
      logging.debug('packagefetcher: skipping %s, fix regex', line)
    fd.close()


def main():
  application = webapp.WSGIApplication([('/packagefetcher', PackageFetcher)], debug=True)
  wsgiref.handlers.CGIHandler().run(application)


if __name__ == '__main__':
  main()
